//
//  FoodStoreListView.swift
//  FoodStore
//
//  Created by youwei on 2025/6/22.
//

import SwiftUI

struct FoodStoreListView: View {
    
    @State private var viewModel = FoodStoreListModel()
    
    var selectedFoodType: FoodTypeItemRespModel?
    var searchContent: String = ""
    
    @Environment(PopupModel.self) private var popup
    @Environment(IndicatorModel.self) private var indicator
    @Environment(LocationService.self) private var locationService: LocationService
    @Environment(NetworkMonitor.self) private var networkMonitor
    
    var body: some View {
        // 店铺列表
        RefreshableLoadMoreScrollView(
            isLoadingMore: viewModel.isLoadingMore,
            hasMore: viewModel.hasMore,
            itemSpace: 12.0)
        {
            ForEach(viewModel.foodStores, id: \.id) { model in
                FoodStoreItemView(model: model)
                    .padding(.horizontal, 12.0)
            }
        } onRefresh: {
            Log.debug("下拉刷新")
            refreshFoodStoreList()
        } onLoadMore: {
            Log.debug("加载更多")
            getMoreFoodStoreList()
        }
        .onAppear {
            refreshFoodStoreList()
        }
        .onChange(of: selectedFoodType) { _, _ in
            refreshFoodStoreList()
        }
        .onChange(of: searchContent) { _, _ in
            refreshFoodStoreList()
        }
        .onChange(of: locationService.locationCoordinate) { oldValue, newValue in
            if newValue != oldValue {
                refreshFoodStoreList()
            }
        }
        .onChange(of: networkMonitor.isConnected) { oldValue, newValue in
            if newValue {
                refreshFoodStoreList()
            }
        }
    }
    
    private func refreshFoodStoreList() {
        Task {
            do {
                indicator.show(content: .loading)
                try await viewModel.getFoodStoreList(
                    searchContent: searchContent,
                    type: selectedFoodType,
                    longitude: locationService.longitude,
                    latitude: locationService.latitude)
                indicator.hide()
            } catch let error as BaseError {
                indicator.hide()
                popup.show(content: error.message)
            } catch {
                // 其它错误
                indicator.hide()
                popup.show(content: "未知错误")
            }
        }
    }
    
    private func getMoreFoodStoreList() {
        Task {
            do {
                try await viewModel.getMoreStoreList(
                    searchContent: searchContent,
                    type: selectedFoodType,
                    longitude: locationService.longitude,
                    latitude: locationService.latitude
                )
            } catch let error as BaseError {
                popup.show(content: error.message)
            } catch {
                // 其它错误
                popup.show(content: "未知错误")
            }
        }
    }
}

#Preview {
    FoodStoreListView()
}
